This page last changed on Oct 24, 2006 by cholmes.

The second type of unit tests are known as Http Unit tests. They are

JUnit tests which are run against a running instance of GeoServer

. An http unit test works by making a request to the server and examining the response. This form of testing is very similar to the cite tests described below. The http unit tests are located in the httptest/ directory of the source tree.

Running the http unit tests requires a number of steps.

  1. Edit the httptest.properties file which contains the paramters needed to locate the running geoserver instance. The file is located in the root of the source tree and contains the following properties:
    protocol Protcol used by the sevrver, default is 'http'
    server IP address or DNS resolvable name of the server, default is 'localhost'
    port Port used for communication with the server, default is '8080'
    context Web Context under which geoserver is located, default is 'geoserver'
  2. Start the geoserver instance.
  3. Execute the unit-http ant task from the root of the source tree.
    [geoserver]% ant unit-http

Here is an example of a simple Http Unit test. For more complete documentation check out httpunit.sourceforge.net.

Writing http unit tests

Here is a quick and dirty example of an HttpUnit test that tests a WMS GetCapabilities request.

public class WMSCapabiltiesTest extends TestCase {

    public void testGetCapabilities() throws Exception {
        WebConversation conversation = new WebConversation();
        WebRequest request = 
        	new GetMethodWebRequest("http://localhost:8080/geoserver/wms?request=getCapabilities");
        
        WebResponse response = conversation.getResponse( request );
        DOMParser parser = new DOMParser();
        parser.parse(new InputSource(response.getInputStream()));
        
        Element e = parser.getDocument().getDocumentElement();
        assertEquals("WMT_MS_Capabilities", e.getLocalName());
    }
}

Pretty simple. The best part is that Http Unit tests are just normal JUnit tests so they are easy to run. All that is requred is the httpjunit.jar available from httpunit.sourceforge.net. The neccessary libraries are also bundled with Geoserver.

As a convenience, an abstract test class AbstractGeoserverHttpTest has been written which http tests can be extended from. This class reads the paramaters needed to connect to the running geoserver instance from the httpunit.properties file located in the root of the source tree. This allows http unit tests to easily be run against a geoserver instance running remotley.

The AbstractGeoserverHttpTest class convenience methods for getting a the parameters used to set up the connection. The above test could be rewritten as:

public class CapabiltiesTest extends AbstractGeoserverHttpTest {

    public void testGetCapabilities() throws Exception {
        WebConversation conversation = new WebConversation();

        //use super.getBaseUrl() instead of hardcoding url
        WebRequest request = 
        	new GetMethodWebRequest(getBaseUrl()+"/wms?request=getCapabilities");
        
        WebResponse response = conversation.getResponse( request );
        DOMParser parser = new DOMParser();
        parser.parse(new InputSource(response.getInputStream()));
        
        Element e = parser.getDocument().getDocumentElement();
        assertEquals("WMT_MS_Capabilities", e.getLocalName());
    }
}
Document generated by Confluence on Jan 16, 2008 23:26